package com.kika.obwiki.job;

import com.kika.obwiki.service.IDocService;
import com.kika.obwiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class DocJob {

  private static final Logger LOG = LoggerFactory.getLogger(DocJob.class);

  @Resource
  private IDocService docService;

  @Resource
  private SnowFlake snowFlake;

  /**
   * 定时批量更新电子书统计信息（文档数、阅读数、点赞数）
   * 每30秒执行一次，兼顾性能与实时性
   * 
   * @author cr
   */
  @Scheduled(cron = "5/30 * * * * ?")
  public void cron() {
    // 增加日志流水号
    MDC.put("LOG_ID", String.valueOf(snowFlake.nextId()));
    LOG.info("更新电子书下的文档数据开始");
    long start = System.currentTimeMillis();
    docService.updateEbookInfo();
    LOG.info("更新电子书下的文档数据结束，耗时：{}毫秒", System.currentTimeMillis() - start);
  }
}